//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package servlet.property;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import utils.JDBCUtils;

@WebServlet({"/GetWorkJson"})
public class GetWorkJson extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public GetWorkJson() {
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/json; charset=utf-8");
        String dormitory = "";
        String building = "";
        if (request.getParameter("building") != null) {
            building = new String(request.getParameter("building").getBytes("ISO-8859-1"), "UTF-8");
        }

        int page = 1;
        int limit = 15;
        if (request.getParameter("page") != null) {
            page = Integer.parseInt(request.getParameter("page"));
        }

        if (request.getParameter("limit") != null) {
            limit = Integer.parseInt(request.getParameter("limit"));
        }

        int count = 0;
        int percount = 0;
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        if (session.getAttribute("username") != null && !session.getAttribute("username").toString().equals("")) {
            Connection connection = null;
            Statement statement = null;
            ResultSet resultSet = null;

            try {
                connection = JDBCUtils.getConnection();
                statement = connection.createStatement();
                String sqltemp = "select count(*) totalCount from homework";
                if (building != "") {
                    sqltemp = sqltemp + " where Bno = '{buidling}' OR belongSchool Like '%{key}%'".replace("{buidling}", building);
                }

                ResultSet rset = statement.executeQuery(sqltemp);
                int rowCount = 0;
                if (rset.next()) {
                    rowCount = rset.getInt("totalCount");
                }

                String sql = "select * from homework";
                if (building != "") {
                    sql = sql + " where Bno = '{buidling}' AND OR belongSchool Like '%{key}%'".replace("{buidling}", building);
                }

                resultSet = statement.executeQuery(sql);
                JSONObject jsonmain = new JSONObject();
                jsonmain.put("code", "200");
                jsonmain.put("msg", "none");
                jsonmain.put("count", rowCount);
                JSONArray jsonarray = new JSONArray();
                JSONObject jsonobj = new JSONObject();

                while(resultSet.next()) {
                    if (count++ >= (page - 1) * limit) {
                        if (percount > limit) {
                            break;
                        }

                        jsonobj.put("_pname", resultSet.getString("pname"));
                        jsonobj.put("_tname", resultSet.getString("tname"));
                        jsonobj.put("_time", resultSet.getString("time"));
                        jsonarray.add(jsonobj);
                        ++count;
                        ++percount;
                    }
                }

                jsonmain.put("data", jsonarray);
                out.println(jsonmain);
            } catch (ClassNotFoundException var25) {
                var25.printStackTrace();
            } catch (SQLException var26) {
                SQLException e = var26;
                e.printStackTrace();
            } finally {
                JDBCUtils.release(resultSet, statement, connection);
            }

        } else {
            response.setStatus(403);
            out.println("403 Forbidden");
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
